﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessPlant
    {
        #region Create
        public static void createPlant(String name, String type, String description, String image)
        {
            try
            {
                Plant p = new Plant();
                p.Name = name;
                p.Type = type;
                p.Description = description;
                p.State = true;
                //p.Image = image;

                dataPlant.createPlant(p);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating plant");
            }
        }

        public static void createPlant(String name,
                                       String type,
                                       String image,
                                       String scientificName,
                                       String botanicalFamily,
                                       String otherNames,
                                       String description,
                                       String uses,
                                       String environment,
                                       Boolean foliage,
                                       Boolean flowering,
                                       String season,
                                       Boolean fragrant,
                                       String growingEase,
                                       String growthRate,
                                       String temperature,
                                       String humidity,
                                       String lightingneeds,
                                       String soiltype,
                                       String watering,
                                       String fertilizer,
                                       String pests,
                                       String propagation,
                                       String toxicity,
                                       Boolean isGenus,
                                       String flowerColor,
                                       String height,
                                       String width)
        {
            try
            {
                Plant p = new Plant();
                p.Name = name;
                p.Type = type;
                //p.Image = image;
                p.ScientificName = scientificName;
                p.BotanicalFamily = botanicalFamily;
                p.OtherNames = otherNames;
                p.Description = description;
                p.Uses = uses;
                p.Environment = environment;
                p.Foliage = foliage;
                p.Flowering = flowering;
                p.Season = season;
                p.Fragrant = fragrant;
                p.GrowingEase = growingEase;
                p.GrowthRate = growthRate;
                p.Temperature = temperature;
                p.Humidity = humidity;
                p.LightingNeeds = lightingneeds;
                p.SoilType = soiltype;
                p.Watering = watering;
                p.Fertilizer = fertilizer;
                p.Pests = pests;
                p.Propagation = propagation;
                p.Toxicity = toxicity;
                p.IsGenus = isGenus;
                p.FlowerColor = flowerColor;
                p.Height = height;
                p.Width = width;
                p.State = true;

                dataPlant.createPlant(p);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating plant");
            }
        }



        #endregion

        #region Get
        public static Plant getPlant(Guid guid)
        {
            try
            {
                return dataPlant.getPlant(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting plant");
            }
        }

        public static List<Plant> getPlantsByName(String name)
        {
            try
            {
                Expression<Func<Plant, bool>> whereClause = (Plant p) => (p.Name.Contains(name)) || (p.OtherNames.Contains(name));
                return dataPlant.getPlants(whereClause).ToList<Plant>();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting plant by name");
            }
        }

        public static List<Plant> getPlantsBySeason(String season)
        {
            try
            {
                Expression<Func<Plant, bool>> whereClause = (Plant p) => (p.Season.Equals(season));
                return dataPlant.getPlants(whereClause).ToList<Plant>();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting by season");
            }
        }

        public static List<Plant> getPlantsByUse(String use)
        {
            try
            {
                Expression<Func<Plant, bool>> whereClause = (Plant p) => (p.Uses.Contains(use));
                return dataPlant.getPlants(whereClause).ToList<Plant>();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting plant by use");
            }
        }

        public static List<Disease> getDiseasesByPlant(Plant p)
        {
            try
            {
                List<Disease> diseases = new List<Disease>();

                Expression<Func<Plants_Disease, bool>> whereClause = (Plants_Disease p_d) => (p_d.PlantID == p.PlantID);
                List<Plants_Disease> pd = dataPlant.getPlant_Disease(whereClause).ToList();

                foreach (Plants_Disease p_d in pd)
                {
                    Disease d = dataDisease.getDisease(p_d.DiseaseID);

                    if (!diseases.Contains(d))
                    {
                        diseases.Add(d);
                    }
                }

                return diseases;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting diseases by plant");
            }
        }
        
        public static List<Method> getMethodByPlant(Plant p)
        {
            try
            {
                List<Method> methods = new List<Method>();

                Expression<Func<Plants_Method, bool>> whereClause = (Plants_Method p_m) => p_m.PlantID == p.PlantID;
                List<Plants_Method> pm = dataPlant.getPlant_Method(whereClause).ToList();

                foreach (Plants_Method e in pm)
                {
                    Method m = dataMethod.getMethod(e.MethodID);

                    methods.Add(m);
                }

                return methods;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting method by plant");
            }
        }

        public static List<Method> getMethodByPlantAndType(Plant p, String type)
        {
            try
            {
                List<Method> methods = new List<Method>();

                Expression<Func<Plants_Method, bool>> whereClause = (Plants_Method p_m) => (p_m.PlantID == p.PlantID) && (p_m.Type.Equals(type));
                List<Plants_Method> pm = dataPlant.getPlant_Method(whereClause).ToList();

                foreach (Plants_Method e in pm)
                {
                    Method m = dataMethod.getMethod(e.MethodID);

                    methods.Add(m);
                }

                return methods;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting method by plant and type");
            }
        }

        public static List<Pathology> getPathologiesByPlant(Plant p)
        {
            try
            {
                List<Pathology> pathologies = new List<Pathology>();

                Expression<Func<Plants_Pathology, bool>> whereClause = (Plants_Pathology p_p) => (p_p.PlantID == p.PlantID);
                List<Plants_Pathology> pp = dataPlant.getPlant_Pathology(whereClause).ToList();

                foreach (Plants_Pathology p_p in pp)
                {
                    Pathology pa = dataPathology.getPathology(p_p.PathologyID);

                    if (!pathologies.Contains(pa))
                    {
                        pathologies.Add(pa);
                    }
                }

                return pathologies;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting pathologies by plant");
            }
        }
        #endregion
    }
}
